package com.lemon.funplus;

import java.util.ArrayList;

import android.app.ProgressDialog;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.lemon.funplus.parse.JsonParser;
import com.lemon.funplus.ulti.BaseEngine;
import com.lemon.funplus.ulti.PreferenceUtils;
import com.lemon.funplus.ulti.TagName;

public class AccountChangePassActivity extends BaseActivity {

	Resources resources;
	EditText edtPass, edtRepass, edtOldpass;
	TextView textCaution;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.account_changepass);
		initView();
	}

	@Override
	protected void onResume() {
		super.onResume();
		resources = getResources();
		menuFragment.ACCOUNT.select();
		myActionBar.setTitle(resources.getString(R.string.changepass));
	}

	void initView() {
		edtOldpass = (EditText) findViewById(R.id.changepass_edittext_oldpass);
		edtPass = (EditText) findViewById(R.id.changepass_edittext_password);
		edtRepass = (EditText) findViewById(R.id.changepass_edittext_repassword);
		findViewById(R.id.changepass_button).setOnClickListener(clickListener);
		textCaution=(TextView) findViewById(R.id.changepass_textView);
		edtRepass.setOnEditorActionListener(new EditText.OnEditorActionListener() {
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				if (actionId == EditorInfo.IME_ACTION_DONE) {
					changepass();
				}
				return false;
			}
		});
	}

	OnClickListener clickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.changepass_button:
				changepass();
				break;
			default:
				break;
			}
		}
	};

	public void changepass() {
		if (!edtOldpass.getText().toString().equals("") && !edtPass.getText().toString().equals("") && !edtRepass.getText().toString().equals("")) {
			new ChangePassAsync().execute("");
		} else
			Toast.makeText(AccountChangePassActivity.this, "Bạn phải nhập đủ thông tin!", Toast.LENGTH_SHORT).show();
	}

	public class ChangePassAsync extends AsyncTask<String, String, String> {
		ProgressDialog progressDialog = new ProgressDialog(AccountChangePassActivity.this);

		@Override
		protected void onPreExecute() {
			if (!progressDialog.isShowing())
				progressDialog.show();
		}

		@Override
		protected String doInBackground(String... arg0) {
			String phone = PreferenceUtils.getString(AccountChangePassActivity.this, TagName.user_name);
			String oldpass = edtOldpass.getText().toString();
			String newpass = edtPass.getText().toString();
			String renewpass = edtRepass.getText().toString();

			String str = JsonParser.changePass(phone, oldpass, newpass, renewpass);
			return str;
		}

		@Override
		protected void onPostExecute(String result) {
//			BaseEngine.showLogformLongString("", result);
			if (progressDialog.isShowing())
				progressDialog.hide();
			if (result != null)
				try {
					ArrayList<String> list = JsonParser.readStatus(result);
					if (list != null&& list.size()>0) {
//						if (list.get(0).equals(CMD.TAG_SUCCESS)) {
							textCaution.setText(list.get(1));
//						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
		}
	}
	// @Override
	// public void onBackPressed() {
	// super.onBackPressed();
	// overridePendingTransition(R.anim.main, android.R.anim.slide_out_right);
	// }
}
